package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.Branch;
import com.mbc.common.entities.BranchRpt;
import com.mbc.common.entities.BranchType;
import com.mbc.common.entities.Country;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.Modal;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.shared.HttpStatus;

public class FrmBranchRpt extends Composite {

	@UiField
	MTextBox txtfromBranchID;
	@UiField
	MTextBox txttoBranchID;
	@UiField
	MTextBox txtfromCountry;
	@UiField
	MTextBox txttoCountry;
	@UiField
	MTextBox txtfromType;
	@UiField
	MTextBox txttoType;

	@UiField
	MButton btnPreview;
	@UiField
	MButton btnClear;
	@UiField
	MButton btnBack;

	@UiField
	MButton btnfromBranchID;
	@UiField
	MButton btntoBranchID;
	@UiField
	MButton btnfromCountry;
	@UiField
	MButton btntoCountry;
	@UiField
	MButton btnfromType;
	@UiField
	MButton btntoType;

	@UiField
	RadioButton rdoBranchID;
	@UiField
	RadioButton rdoCountry;
	@UiField
	RadioButton rdoType;

	private Branch branch;
	private Country country;
	private BranchType branchtype;

	byte action_type;

	private static FrmBranchRptUiBinder uiBinder = GWT
			.create(FrmBranchRptUiBinder.class);

	interface FrmBranchRptUiBinder extends UiBinder<Widget, FrmBranchRpt> {
	}

	public FrmBranchRpt() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	private void initialize() {
		
		txtfromBranchID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoBranchID.getElement().setAttribute("style", "text-transform: uppercase;");
		txtfromCountry.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoCountry.getElement().setAttribute("style", "text-transform: uppercase;");
		txtfromType.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoType.getElement().setAttribute("style", "text-transform: uppercase;");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				txtfromBranchID.setFocus(true);
			}
		});
		
        txttoType.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					event.preventDefault();
					TextBox l_txt = (TextBox) event.getSource();
					if (l_txt == txttoType)
						btnPreview.setFocus(true);
				}
			}
		});

		KeyDownHandler l_rdo_handler = new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				RadioButton l_rdo = (RadioButton) event.getSource();
				int code = event.getNativeKeyCode();
				if (event.isShiftKeyDown() && code == KeyCodes.KEY_TAB) {
					event.preventDefault();
					if (l_rdo == rdoBranchID)
						rdoBranchID.setFocus(true);
					else if (l_rdo == rdoCountry)
						rdoBranchID.setFocus(true);
					else if (l_rdo == rdoType)
						rdoCountry.setFocus(true);
				} else if (code == KeyCodes.KEY_ENTER
						|| code == KeyCodes.KEY_TAB) {
					event.preventDefault();
					if (l_rdo == rdoBranchID)
						rdoCountry.setFocus(true);
					else if (l_rdo == rdoCountry)
						rdoType.setFocus(true);
					else if (l_rdo == rdoType)
						btnPreview.setFocus(true);
				}
			}
		};
		rdoBranchID.addKeyDownHandler(l_rdo_handler);
		rdoCountry.addKeyDownHandler(l_rdo_handler);
		rdoType.addKeyDownHandler(l_rdo_handler);
		rdoBranchID.setValue(true);

	}

	private BranchRpt bindBranchRpt() {
		BranchRpt l_info = new BranchRpt();
		l_info.setFromBranchID(txtfromBranchID.getText());
		l_info.setToBranchID(txttoBranchID.getText());

		l_info.setFromCountry(txtfromCountry.getText());
		l_info.setToCountry(txttoCountry.getText());

		l_info.setFromType(txtfromType.getText());
		l_info.setToType(txttoType.getText());

		if (rdoBranchID.getValue() == true)
			l_info.setOrderby("BranchID");
		else if (rdoCountry.getValue() == true)
			l_info.setOrderby("Country");
		else if (rdoType.getValue() == true)
			l_info.setOrderby("Type");

		return l_info;
	}

	@UiHandler("btnPreview")
	void onBtnPreviewClick(ClickEvent event) {
		ViewUtils.closeMessageBox();
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.prepareBranchReport(bindBranchRpt(),
				BranchReportCallback());
	}

	private AsyncCallback<Integer> BranchReportCallback() {
		return new AsyncCallback<Integer>() {

			@Override
			public void onSuccess(Integer result) {
				ViewUtils.stopAjaxLoader();
				if (result == HttpStatus.OK) {
					ClientSingleton.viewReport("Branch Report");
				} else if (result == HttpStatus.NOT_FOUND) {
					ViewUtils.showErrorMessage("No records.");
				}
			}

			@Override
			public void onFailure(Throwable caught) {

			}
		};
	}

	@UiHandler("btnfromBranchID")
	void onbtnfromBranchIDClick(ClickEvent event) {
		final SearchBranch l_dia = new SearchBranch("Branch List","");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					branch = l_dia.getSelectedEntity();
					txtfromBranchID.setText(branch.getBranchCode());

					txttoBranchID.setFocus(true);
				} else
					txtfromBranchID.setFocus(true);
			}
		});
	}

	@UiHandler("btntoBranchID")
	void onbtntoBranchIDClick(ClickEvent event) {
		final SearchBranch l_dia = new SearchBranch("Branch List","");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					branch = l_dia.getSelectedEntity();
					txttoBranchID.setText(branch.getBranchCode());

					txtfromBranchID.setFocus(true);
				} else
					txttoBranchID.setFocus(true);
			}
		});
	}

	@UiHandler("btnfromCountry")
	void onBtnBwrfromCountryClick(ClickEvent event) {
		final SearchCountry l_dia = new SearchCountry("Country List");
		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					country = l_dia.getSelectedEntity();
					txtfromCountry.setText(country.getName());
					txttoCountry.setFocus(true);
				} else
					txtfromCountry.setFocus(true);
			}

		});
	}

	@UiHandler("btntoCountry")
	void onbtntoCountryClick(ClickEvent event) {
		final SearchCountry l_dia = new SearchCountry("Country List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					country = l_dia.getSelectedEntity();
					txttoCountry.setText(country.getName());

					txttoCountry.setFocus(true);
				} else
					txttoCountry.setFocus(true);
			}
		});
	}

	@UiHandler("btnfromType")
	void onBtnBwrfromTypeClick(ClickEvent event) {
		final SearchBranchType l_dia = new SearchBranchType("Branch Type List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					branchtype = l_dia.getSelectedEntity();
					txtfromType.setText(branchtype.getName());
					txttoType.setFocus(true);
				} else
					txtfromType.setFocus(true);
			}

		});
	}

	@UiHandler("btntoType")
	void onbtntoTypeClick(ClickEvent event) {
		final SearchBranchType l_dia = new SearchBranchType("Branch Type List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					branchtype = l_dia.getSelectedEntity();
					txttoType.setText(branchtype.getName());

					txttoType.setFocus(true);
				} else
					txttoType.setFocus(true);
			}
		});
	}

	@UiHandler("btnClear")
	void onBtnClearClick(ClickEvent event) {
		txtfromBranchID.setText("");
		txttoBranchID.setText("");
		txtfromCountry.setText("");
		txttoCountry.setText("");
		txtfromType.setText("");
		txttoType.setText("");
		txtfromBranchID.setFocus(true);
	}
	
	@UiHandler("txtfromBranchID")
	void onTxtFromBranchChange(ChangeEvent event) {
		if (!txtfromBranchID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchByCode(txtfromBranchID.getText(),
					getFromBranchCallback());
	}

	private AsyncCallback<Branch> getFromBranchCallback() {
		return new AsyncCallback<Branch>() {

			@Override
			public void onSuccess(Branch result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromBranchID.setText(result.getBranchCode());
					txtfromBranchID.getElement().setAttribute("id",
							String.valueOf(result.getBranchId()));
				} else {
					txtfromBranchID.setText("");
					txtfromBranchID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Branch Code does not exist.");
					txtfromBranchID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoBranchID")
	void onTxtToBranchChange(ChangeEvent event) {
		if (!txttoBranchID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchByCode(txttoBranchID.getText(),
					getToBranchCallback());
	}

	private AsyncCallback<Branch> getToBranchCallback() {
		return new AsyncCallback<Branch>() {

			@Override
			public void onSuccess(Branch result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoBranchID.setText(result.getBranchCode());
					txttoBranchID.getElement().setAttribute("id",
							String.valueOf(result.getBranchId()));
				} else {
					txttoBranchID.setText("");
					txttoBranchID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Branch Code does not exist.");
					txttoBranchID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txtfromCountry")
	void onTxtFromCountryChange(ChangeEvent event) {
		if (!txtfromCountry.getText().equals("")){
			Country l_info = new Country();
			l_info.setName(txtfromCountry.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCountryByCode(l_info ,
					getFromCountryCallback());
		}
	}

	private AsyncCallback<Country> getFromCountryCallback() {
		return new AsyncCallback<Country>() {

			@Override
			public void onSuccess(Country result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromCountry.setText(result.getName());
					txtfromCountry.getElement().setAttribute("id",
							String.valueOf(result.getCountryId()));
				} else {
					txtfromCountry.setText("");
					txtfromCountry.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Country Name does not exist.");
					txtfromCountry.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoCountry")
	void onTxtToCountryChange(ChangeEvent event) {
		if (!txttoCountry.getText().equals("")){
			Country l_info = new Country();
			l_info.setName(txttoCountry.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCountryByCode(l_info ,
					getToCountryCallback());
		}
	}

	private AsyncCallback<Country> getToCountryCallback() {
		return new AsyncCallback<Country>() {

			@Override
			public void onSuccess(Country result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoCountry.setText(result.getName());
					txttoCountry.getElement().setAttribute("id",
							String.valueOf(result.getCountryId()));
				} else {
					txttoCountry.setText("");
					txttoCountry.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Country Name does not exist.");
					txttoCountry.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txtfromType")
	void onTxtFromBranchTypeChange(ChangeEvent event) {
		if (!txtfromType.getText().equals("")){
			BranchType l_info = new BranchType();
			l_info.setName(txtfromType.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchTypeByCode(l_info,
					getFromBranchTypeCallback());
		}
	}

	private AsyncCallback<BranchType> getFromBranchTypeCallback() {
		return new AsyncCallback<BranchType>() {

			@Override
			public void onSuccess(BranchType result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromType.setText(result.getName());
					txtfromType.getElement().setAttribute("id",
							String.valueOf(result.getBranchTypeId()));
				} else {
					txtfromType.setText("");
					txtfromType.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Branch Type Name does not exist.");
					txtfromType.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoType")
	void onTxtToBranchTypeChange(ChangeEvent event) {
		if (!txttoType.getText().equals("")){
			BranchType l_info = new BranchType();
			l_info.setName(txttoType.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchTypeByCode(l_info,
					getToBranchTypeCallback());
		}
	}

	private AsyncCallback<BranchType> getToBranchTypeCallback() {
		return new AsyncCallback<BranchType>() {

			@Override
			public void onSuccess(BranchType result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoType.setText(result.getName());
					txttoType.getElement().setAttribute("id",
							String.valueOf(result.getBranchTypeId()));
				} else {
					txttoType.setText("");
					txttoType.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Branch Type Name does not exist.");
					txttoType.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	
	@UiHandler("txtfromBranchID")
	void onTxtfromBranchKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onbtnfromBranchIDClick(null);
		}
	}
	@UiHandler("txttoBranchID")
	void onTxttoBranchKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onbtntoBranchIDClick(null);
		}
	}
	@UiHandler("txtfromCountry")
	void onTxtfromCountryKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrfromCountryClick(null);
		}
	}
	@UiHandler("txttoCountry")
	void onTxttoCountryKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onbtntoCountryClick(null);
		}
	}
	@UiHandler("txtfromType")
	void onTxtfromTypeKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrfromTypeClick(null);
		}
	}
	
	@UiHandler("txttoType")
	void onTxttoTypeKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onbtntoTypeClick(null);
		}
	}
	
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmBranch());
	}
}
